import Vue from 'vue'
import VueRouter from 'vue-router'
import Article from '@/views/article'
import Layout from '@/views/layout'
import Login from '@/views/login'
import Dashboard from '@/views/dashboard'
import store from '@/store/index'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    {
      path: '/',
      component: Layout,
      redirect: '/dashboard',
      children: [
        // 如果子路由的 path，不是以 / 开头，视为相对路径 => 自动拼上父路由的路径
        { path: 'dashboard', component: Dashboard },
        { path: 'article', component: Article }
      ]
    }
  ]
})
// 路由拦截
router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (to.path !== '/login' && !token) return next('/login')
  next()
})
export default router
